package com.bootdo.lgs.order.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.exception.ParmException;
import com.bootdo.common.model.BdConstant;
import com.bootdo.common.utils.R;
import com.bootdo.lgs.combox.service.ComboxService;
import com.bootdo.lgs.order.model.LgsOrder;
import com.bootdo.lgs.order.model.LgsOrderQuery;
import com.bootdo.lgs.order.model.OrderExcel;
import com.bootdo.lgs.order.service.OrderService;
import com.bootdo.system.domain.UserDO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author bootdo 1992lcg@163.com
 */
//@RequestMapping("/lgs/order")
@Controller
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ComboxService comboxService;

    @GetMapping("/lgs/order/preview")
    String previewOrderDetailForPrint(@RequestParam Long id, Model model) {
        model.addAttribute("orderDetail", orderService.getDataById(id));
        return "lgs/order/orderDetailPreview";
    }

    @GetMapping("/lgs/order/details")
    String previewOrderDetails(@RequestParam String[] ids, Model model) {
        model.addAttribute("orderList", orderService.batchQuery(Arrays.stream(ids).filter(NumberUtils::isNumber).map(Long::parseLong).toArray(Long[]::new)));
        return "lgs/order/orderListPreview";
    }

    //跳转 - 录单
    //@RequiresPermissions("sys:menu:menu")
    @GetMapping("/lgs/order")
    String order(Model model) {
        model.addAttribute("companyCheckBoxList", orderService.getComboxByGrpCd());
        return "lgs/order/order";
    }

    //跳转 - 录单
    @GetMapping("/lgs/order/showAdd")
    String showAdd(Model model) {
        setInitParams(model);
        return "lgs/order/add";
    }

    private void setInitParams(Model model) {
        model.addAttribute("companys", comboxService.getComboxByGrpCd(BdConstant.LGS_COMPANY));
        model.addAttribute("transportModes", comboxService.getComboxByGrpCd(BdConstant.TRANSPORT_MODE));
        model.addAttribute("deliveryMethods", comboxService.getComboxByGrpCd(BdConstant.DELIVERY_METHOD));
    }

    //跳转 - 导入
    @GetMapping("/lgs/order/showImport")
    String showImport(Model model) {
        return "lgs/order/import";
    }


    //跳转 - 修改
    @GetMapping("/lgs/order/showEdit")
    String showUpdate(Model model) {
        setInitParams(model);
        return "lgs/order/edit";
    }

    //跳转 - 收货后修改
    @GetMapping("/lgs/order/showEdit2")
    String showUpdate2(@RequestParam Long id, Model model) {
        model.addAttribute("order", orderService.getDataById(id));
        return "lgs/order/edit2";
    }

    //@RequiresPermissions("sys:menu:menu")
    @GetMapping("/lgs/order/list")
    @ResponseBody
    List list(LgsOrderQuery condition) {
        return orderService.getList(condition);
    }

    @Log("录单")
    //@RequiresPermissions("sys:menu:add")
    @PostMapping("/lgs/order/add")
    @ResponseBody
    R add(LgsOrder conditon) {
        UserDO loginUser = getUser();
        orderService.add(conditon, loginUser);
        return R.ok();
    }

    @Log("导入订单")
    @ApiOperation(value = "excel导入 订单", notes = "excel导入 订单")
    @PostMapping(value = "/lgs/order/importExcel")
    @ResponseBody
    public R importExcel(OrderExcel condition, HttpServletRequest request) {
        UserDO loginUser = getUser();
        try {
            orderService.insertBatchByExcel(condition, loginUser);
        } catch (ParmException e) {
            return R.error(1, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();

//		try {
//			ValidationUtils.validate(condition);// 检查参数
//			if (Operate.FAIL == phoneMngService.insertBatchByExcel(condition))
//				return new RespEntity(41,"增加失败！").toJsonStringIgnoreNull();
//		} catch (ParmException e) {
//			return new RespEntity(1,e.getMessage()).toJsonStringIgnoreNull();
//		} catch (Exception e) {
//			if (logger.isErrorEnabled())
//				logger.error("phoneMng excel add error,condition:" + condition, e);
//			return new RespEntity(42, "发生异常，增加失败！").toJsonStringIgnoreNull();
//		}
//		condition.setFile(null);
//		return new RespEntity(RespCode.SUCCESS,condition).toJsonStringIgnoreNull();
    }

    @Log("修改订单")
    //@RequiresPermissions("sys:menu:add")
    @PostMapping("/lgs/order/update")
    @ResponseBody
    R update(LgsOrder conditon) {
        //修改之前判断是否存在
        UserDO loginUser = getUser();
        orderService.update(conditon, loginUser);

        return R.ok();
    }

    @Log("收货后修改订单")
    @PostMapping("/lgs/order/update2")
    @ResponseBody
    R updateAfterDeliver(@RequestBody LgsOrder conditon) {
        return orderService.updateAfterDeliver(conditon, getUser()) ? R.ok() : R.error();
    }

    @Log("删除订单")
    //@RequiresPermissions("sys:menu:add")
    @PostMapping("/lgs/order/remove")
    @ResponseBody
    R remove(Long id) {
        //修改之前判断是否存在
        orderService.delete(id);
        return R.ok();
    }

    @Log("下单")
    //@RequiresPermissions("sys:menu:add")
    @PostMapping("/lgs/order/batchOrder")
    @ResponseBody
    R batchOrder(@RequestBody List<LgsOrder> ids) {
        //修改之前判断是否存在
        UserDO loginUser = getUser();
        orderService.batchUpdate(ids, loginUser);
        return R.ok();
    }

    @ApiOperation(value = "列表-导出", notes = "导出，使用post表单方式提交、进行下载")
    @RequestMapping(value = "/lgs/order/export", method = RequestMethod.POST)
    public void export(LgsOrderQuery condition, HttpServletResponse response) {
        orderService.export(condition, response);
    }
}
